package com.yonyougov.devops.cruxcisupport.envconfigitem;

import com.yonyougov.devops.cruxcisupport.env.dto.config.EnvConfigItemQueryResponseDto;
import com.yonyougov.devops.cruxcisupport.env.vo.config.EnvConfigItemVo;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_env_config_item服务实现
 */
@Service
public class EnvConfigItemServiceImpl extends BaseServiceImpl<EnvConfigItemEntity, BigInteger> implements EnvConfigItemService {
    private final EnvConfigItemDao envConfigItemDao;

    public EnvConfigItemServiceImpl(EnvConfigItemDao envConfigItemDao) {
        this.envConfigItemDao = envConfigItemDao;
    }

    @Override
    public BaseDao<EnvConfigItemEntity, BigInteger> getDao() {
        return envConfigItemDao;
    }

    //<!--用户自定义代码开始-->

    /**
     * 查询环境配置项
     * 条件: EnvConfigItemEntity实体
     *
     * @param envConfigItemEntity [com.yonyougov.devops.cruxcisupport.envconfigitem.EnvConfigItemEntity]
     * @return [com.yonyougov.devops.cruxcisupport.env.dto.config.EnvConfigItemQueryResponseDto]
     */
    @Override
    public EnvConfigItemQueryResponseDto findPageBy(EnvConfigItemEntity envConfigItemEntity) {
        Pagination<EnvConfigItemEntity> pagination = findAllBy(envConfigItemEntity);
        List<EnvConfigItemVo> envConfigItemVoList = new ArrayList<>();
        pagination.getDatas().forEach(entity -> {
            EnvConfigItemVo envConfigItemVo = new EnvConfigItemVo();
            BeanUtils.copyProperties(entity, envConfigItemVo);
            envConfigItemVoList.add(envConfigItemVo);
        });

        return new EnvConfigItemQueryResponseDto(pagination.getTotalCount(), pagination.getPageCount(),
                pagination.getPageSize(), pagination.getPageNum(), envConfigItemVoList);
    }
    //<!--用户自定义代码结束-->
}
